package org.graduation.note.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.note.Note;
import org.graduation.model.common.user.User;
import org.graduation.note.service.NoteService;
import org.graduation.utils.ResponseResultUtil;
import org.graduation.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/note")
public class NoteController {

    @Autowired
    private Token token;
    @Autowired
    private NoteService noteService;

    @PostMapping("/add")
    public ResponseResult addNote(@RequestBody Note note, HttpServletRequest httpServletRequest) {
        int uid = token.getUidByToken(token.getTokenByRequest(httpServletRequest));
        note.setUid(uid);
        boolean isSave = noteService.save(note);
        return ResponseResultUtil.simpleCheckHaveData(isSave, note.getNoteId().toString());
    }

    @PostMapping("/update")
    public ResponseResult addOrUpdateNote(@RequestBody Note note, HttpServletRequest httpServletRequest) {
        int uid = token.getUidByToken(token.getTokenByRequest(httpServletRequest));
        note.setUid(uid);
        return ResponseResultUtil.simpleCheck(noteService.updateNote(note));
    }

    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody Note note) {
        note.setDeleteTime(new Date());
        return ResponseResultUtil.simpleCheck(noteService.updateById(note));
    }

    @DeleteMapping("/realDelete")
    public ResponseResult realDelete(@RequestBody Note note) {
        return ResponseResultUtil.simpleCheck(noteService.removeById(note));
    }

    @GetMapping("/list/{page}")
    public ResponseResult view(@PathVariable Integer page, HttpServletRequest httpServletRequest) {
        int uid = token.getUidByToken(token.getTokenByRequest(httpServletRequest));
        Page<Note> notePage = new Page<>(page, 10);
        return ResponseResult.success(noteService.pageList(notePage, uid).getRecords());
    }

    @GetMapping("/info")
    public ResponseResult info(@RequestParam Long noteId) {
        return ResponseResult.success(noteService.getById(noteId));
    }
    @GetMapping("/info/allAlarmNote")
    public List<Note> allAlarmNote(@RequestParam Integer uid) {
        User user = new User();
        user.setUid(uid);
        return noteService.alarmNote(user);
    }
}
